//
//  HomeViewController.m
//  BigJoy
//
//  Created by new-1020 on 2017/6/12.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "HomeViewController.h"
#import "CommonCollectionViewCell.h"
#import "SectionOneReusableView.h"
#import "CategoryCell.h"
#import "CategoryViewController.h"
#import "AllTypeViewController.h"
#import "XMSearchViewController.h"
#import "SectionTwoReusableView.h"
#import "MoreView.h"

@interface HomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UICollectionView * collectionView;
@property (nonatomic,strong) NSArray *tagImageArray;
@end

@implementation HomeViewController

-(UICollectionView *)collectionView {
    if (_collectionView == nil) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumInteritemSpacing = XMW(10);
        layout.sectionInset = UIEdgeInsetsMake(XMH(10), XMW(10), 0, XMW(10));
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        //register
        [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CommonCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"Cell"];
        [self.collectionView registerNib:[UINib nibWithNibName:@"SectionOneReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UID"];
        [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"kong"];
        [self.collectionView registerNib:[UINib nibWithNibName:@"SectionTwoReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"STWO"];
        [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CategoryCell class]) bundle:nil] forCellWithReuseIdentifier:@"CateCell"];
    }
    return _collectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self cutomNavBarView];
    self.tagImageArray = @[@"shirt",@"goods_fill",@"note",@"gengduoda"];
    [self.view addSubview:self.collectionView];
    self.collectionView.mj_footer = [XMCommonReresh XMRefreshCommonFooterWithSelector:@selector(footerRefresh) AndTaget:self];
    @weakify(self);
    [[self.serchView.searchbutton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        XMSearchViewController * vc = [[XMSearchViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }];
}

-(void)footerRefresh {
    NSLog(@"refresh");
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return  CGSizeMake(SCREEN_WIDTH, 190);
    }else if (section == 1) {
        return  CGSizeMake(SCREEN_WIDTH, 40);
    } else {
        return CGSizeZero;
    }
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return  CGSizeMake((SCREEN_WIDTH - XMW(30)  ) / 2, (SCREEN_WIDTH - XMW(40) ) / 2 * 0.6);
    } else {
        return  CGSizeMake(XMW(170), XMW(240));
    }
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
     SectionOneReusableView * reusableView;
    if (kind==UICollectionElementKindSectionHeader && indexPath.section == 0) {
        reusableView=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UID" forIndexPath:indexPath];
    }else if (kind==UICollectionElementKindSectionHeader && indexPath.section == 1) {
        reusableView=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"STWO" forIndexPath:indexPath];
    }else {
        reusableView=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"kong" forIndexPath:indexPath];
    }
    return reusableView;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 4;
    }
    if (section == 1) {
        return 10;
    }
    return 10;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        CategoryCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CateCell" forIndexPath:indexPath];
        if (indexPath.row == 3) {
            cell.CateType = AllType;
        } else {
            cell.CateType = SingleType;
        }
        cell.imageView.image = [UIImage imageNamed:self.tagImageArray[indexPath.row]];
        return cell;
    }else {
        CommonCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
        return cell;
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (((CategoryCell *)[collectionView cellForItemAtIndexPath:indexPath]).CateType == AllType) {
//            AllTypeViewController * vc = [[AllTypeViewController alloc]init];
//            [self.navigationController pushViewController:vc animated:YES];
            MoreView * moreView = [[MoreView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
            [moreView animationAddToView:XMKEYWINDOWXM];
            return;
        }
        CategoryViewController * vc = [[CategoryViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma -mark:searchBarDelegate
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    XMSearchViewController * vc = [[XMSearchViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    return NO;
}

@end
